﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Bids;
using BLL.Bids.Impl;
using Model;
using GenericLibrariesToolKit;
using Utils;
using BLL.Privilege;
using BLL.Privilege.Impl;
using System.Text;

public partial class Page_Bids_BidsDetail : System.Web.UI.Page
{
    static string bidId = "";
    StringBuilder script = new StringBuilder();
    protected void Page_Load(object sender, EventArgs e)
    {
            bidId = Request["BidId"].ToString();
            bidsPrivilegeControl();
            BidDetailDataBind();
            BidReplyDataBind();
            bulletDatabind();
    }
    //标细节数据绑定
    private void BidDetailDataBind()
    {
        if (bidId != null)
        {
            BidBO bidBO = new BidBOImpl();
            List<Bid> bidList = bidBO.GetEntityList("BidId", bidId, "StartDate");
            dvBidInfo.DataSource = bidList;
            dvBidInfo.DataBind();
            foreach (Bid bid in bidList)
            {
                if (bid.IsExpire == 1)
                {
                    lblBidState.Text = "Closed !";
                }
            }
        }
    }
    /// <summary>
    ///对回复标进行权限控制
    /// </summary>
    public void bidsPrivilegeControl()
    {
        //获取用户ID
        string userID = WebUtil.GetIdentifier(Session["userId"]);
        RoleRightBO roleRightBO = new RoleRightBOImpl();
        //如果用户为游客，返回false
        if (userID == null)
            return;
        Dictionary<string, bool> privilege = new Dictionary<string, bool>();
        privilege.Add("bidsReply", roleRightBO.IsPrivilege(userID, "bidsReply", OperateType.add));
        //privilege.Add("postBulletin", roleRightBO.IsPrivilege(userID, "postBulletin", OperateType.add));       
        //获取该bid的发标人
        BidBO bidBO = new BidBOImpl();
        List<Bid> bidList = bidBO.GetEntityList("BidId", bidId, "StartDate");
        string bidPoster = null;
        foreach (Bid bid in bidList)
        {
            bidPoster = bid.EnterpriseId;
        }
        postBidsUIControl(privilege, bidPoster);
    }
    /// <summary>
    ///对显示进行控制
    /// </summary>
    private void postBidsUIControl(Dictionary<string, bool> privilege, string posterId)
    {
        //专家模块
        if (privilege["bidsReply"])
        {          
            script.Append("<script>");
            script.Append("document.getElementById('BidBulletin').style.display='none';");        
        }
        //企业模块
        else
        {
            script.Append("<script>");
            script.Append("document.getElementById('ReplyBid').style.display='none';");
            if (posterId != WebUtil.GetIdentifier(Session["userId"]))
            {
                script.Append("document.getElementById('BidBulletin').style.display='none';");              
            }         
        }
}
    //竞标回复信息数据绑定
    private void BidReplyDataBind()
    {
        if (bidId != null)
        {
            BidReplyBO bidReplyBO = new BidReplyBOImpl();
            List<BidReply> bidReplyList = bidReplyBO.GetEntityList("BidId", bidId,"BidReplyDate");
            GVBidReply.DataSource = bidReplyList;
            GVBidReply.DataBind();
            DropDownWinnerDataBind(bidReplyList);
        }
    }
    //竞标者下拉列表框数据绑定
    private void DropDownWinnerDataBind(List<BidReply> bidReplyList)
    {
        foreach (BidReply bidReply in bidReplyList)
        {
            if (DDWinner.Items.FindByText(bidReply.ExpertId) == null)
            {
                DDWinner.Items.Add(bidReply.ExpertId);
            }
        }

    }

    //竞标按钮事件
    protected void btnReplyBidSubmit_Click(object sender, ImageClickEventArgs e)
    {
        string replyDetails = txtReplyBid.Text.Trim();
        BidReply bidReply = new BidReply()
        {
            BidId = bidId,
            ExpertId = WebUtil.GetIdentifier(Session["userId"]),
            ReplyDetail = replyDetails
        };
        BidReplyBO bidReplyBO = new BidReplyBOImpl();
        bool result = bidReplyBO.Add(bidReply);
        if (result)
        {
            Response.Write("<script language=javascript>alert('success！')</script>");
            Response.Write("<script language=javascript>window.location.href=document.URL;</script>");
        }
        else
        {
            Response.Write("<script>alert('Reply Fail')</script>");
        }
    }
    //竞标专家链接，跳转到专家详细信息页面
    protected void LBBidder_Click(object sender, EventArgs e)
    {
        LinkButton bt = (LinkButton)sender;
        string expertId = bt.CommandArgument.ToString();
        Response.Redirect("../Expert/ExpertDetails.aspx?expertId=" + expertId);
    }
    //发标标的后续信息，包括对标的补充和竞标公告
    protected void btnBidsPlusInfoSubmit_Click(object sender, ImageClickEventArgs e)
    {
        string bulletTitle = txtBulletTitle.Text.Trim();
        string bulletDetail = txtBulletDetail.Text.Trim();
        string bidderWinner = DDWinner.Text.Trim();
        if (bulletTitle == null || bulletTitle == "")
        {
            lTitle.Text = "Required.";
            return;
        }
        if (bulletDetail == null || bulletDetail == "")
        {
            lDetail.Text = "Required.";
            return;
        }
        //选中标人，更新标
        if (bidderWinner != "none")
        {
            BidBO bidBO = new BidBOImpl();
            Bid bid = bidBO.Find(bidId);
            bid.ExpertId = bidderWinner;
            bid.IsExpire = 1;
            bidBO.Update(bid);

        }
        BidBulletin bidBullet = new BidBulletin()
        {
            BidId = bidId,
            BulletinTitle = bulletTitle,
            BulletinDetail = bulletDetail
        };
        BidBulletinBO bidBulletBO = new BidBulletinBOImpl();
        bool result = bidBulletBO.Add(bidBullet);
        if (result)
        {
            Response.Write("<script language=javascript>alert('success！')</script>");
            Response.Write("<script language=javascript>window.location.href=document.URL;</script>");
        }
        else
        {
            Response.Write("<script>alert('Fail')</script>");
        }
    }
    //标的补充信息数据绑定
    private void bulletDatabind()
    {
        BidBulletinBO bidBulletBO = new BidBulletinBOImpl();
        List<BidBulletin> bidBulletList = bidBulletBO.GetEntityList("BidId", bidId,"BulletinDate");
        if (bidBulletList.Count < 1)
        {            
            script.Append("document.getElementById('BidsPlusInfo').style.display='none';");          
        }
        script.Append("</script>");
        Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());
        GVbullet.DataSource = bidBulletList;
        GVbullet.DataBind();

    }
    //返回按钮事件
    protected void btnBidInfoBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("<script language=javascript>history.go(-2);</script>");

    }
    protected void GVbullet_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GVbullet.PageIndex = e.NewPageIndex;
        this.GVbullet.DataBind();
    }

    protected void GVBidReply_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GVBidReply.PageIndex = e.NewPageIndex;
        this.GVBidReply.DataBind();
    }
}